let alertTemplate = '<div id="xdalert" class="alert alert-primary" role="alert">\n' +
    '<span id="xdalert_content">  A simple primary alert—check it out!</span>\n' +
    '<button type="button" class="close" data-dismiss="alert" aria-label="Close">\n' +
    '    <span aria-hidden="true">&times;</span>\n' +
    '  </button>' +
    '</div>';
let toastTemplate = '<div id="xdtoast" role="alert" aria-live="assertive" aria-atomic="true" class="toast" data-autohide="true" style="padding:0 20px;position:absolute;right: 10px;top: 10px;">\n' +
    '      <div class="toast-header">\n' +
    '        <strong class="mr-auto">{{ title }}</strong>\n' +
    '        <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">\n' +
    '          <span aria-hidden="true">&times;</span>\n' +
    '        </button>\n' +
    '      </div>\n' +
    '      <div class="toast-body">\n' +
    '        {{ content }}\n' +
    '      </div>\n' +
    '    </div>';
let loadingTemplate = '<div id="xdloading" class="spinner-grow text-primary" role="status" style="position: absolute;left: 50%;top: 50%;margin-left:25px;margin-top: 25px;">\n' +
    '  <span class="sr-only">Loading...</span>\n' +
    '</div>';
let buttonLoadingTemplate = '<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>\n' +
        '  <span>Loading...</span>';
let confirmTemplate = '<div id="xdconfirm" class="modal" tabindex="-1" role="dialog">\n' +
    '  <div class="modal-dialog" role="document">\n' +
    '    <div class="modal-content">\n' +
    '      <div class="modal-header">\n' +
    '        <h5 class="modal-title">Modal title</h5>\n' +
    '        <button type="button" class="close" data-dismiss="modal" aria-label="Close">\n' +
    '          <span aria-hidden="true">&times;</span>\n' +
    '        </button>\n' +
    '      </div>\n' +
    '      <div class="modal-body">\n' +
    '        <p>Modal body text goes here.</p>\n' +
    '      </div>\n' +
    '      <div class="modal-footer">\n' +
    '        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>\n' +
    '        <button type="button" class="btn btn-primary">Save changes</button>\n' +
    '      </div>\n' +
    '    </div>\n' +
    '  </div>\n' +
    '</div>';
var st = null;
var utils = {
    alert : function (content,  size , interval){
        if($("#xdalert").length <= 0){
            $("body").before(alertTemplate);
        }
        clearTimeout(st);
        $('.alert #xdalert_content').text(content || '提示内容');
        if('mini' == size){
            $("#xdalert").attr("style","width:200px;margin: 5px auto;");
        }else{
            $("#xdalert").attr("style","margin: 5px 100px;");
        }
        $('.alert').alert()
        st = setTimeout(function(){
            $('.alert').alert('close')
            clearTimeout(st);
        },interval || 2000)
    },
    toast : function (content,title){
        if($("#xdtoast").length <= 0){
            $("body").append(toastTemplate);
        }
        $('.toast .mr-auto').text(title || '提示');
        $('.toast .toast-body').text(content || '提示内容');
        $('.toast').toast('show')
    },
    loading : function(show){
        if($("#xdloading").length <= 0){
            $("body").append(loadingTemplate);
        }
        if(false == show){
            $("#xdloading").hide()
        }else{
            $("#xdloading").show()
        }
    },
    btnLoading : function(id, show){
        if(false == show) {
            let value = this.getCache(id);
            $('#' + id).attr("disabled", false);
            $('#' + id).html(value);
            this.removeCache()
        }else{
            this.setCache(id,$('#' + id).html());
            $('#' + id).attr("disabled", true);
            $('#' + id).html(buttonLoadingTemplate);
        }
    },
    confirm : function( title, content , successCallback , cancelCallback , option){
        if($("#xdconfirm").length <= 0){
            $("body").append(confirmTemplate);
        }
        $('.modal .modal-title').text(title || '提示');
        $('.modal .modal-body').text(content || '提示内容');
        $('.modal .btn-secondary').text(option && option.closeBtn || '关闭');
        $('.modal .btn-primary').text(option && option.okBtn || '确定');

        $('.modal').modal('show')

        if(successCallback){
            var btn = $("button.btn-primary")[0];
            if ( btn.addEventListener ) {
                btn.addEventListener("click", function(){
                    successCallback();
                    $('.modal').modal('hide')
                }, false);
            } else { // IE
                btn.attachEvent("onclick", function(){
                    successCallback();
                    $('.modal').modal('hide')
                });
            }
        }
    },
    validate: function(id){
        let t = true;
        $("form" + id + " input,checkbox,select").each(function(){
            let value = $(this).val();
            let validReg = $(this).attr('validReg');
            let validMessage = $(this).attr('validMessage');
            let required = $(this).attr("required");
            if(required){
                if(null == value || '' == value){
                    t = false;
                    $(this).focus();
                    utils.alert( '该录入项为必填项！');
                    return false;
                }
            }
            if(validReg){
                var reg= new RegExp(validReg);
                // console.log(validReg)
                // console.log(reg)
                if(!reg.test(value)){
                    t = false;
                    $(this).focus();
                    utils.alert(validMessage || '该输入信息有误！');
                    return false;
                }
            }


        });
        return t;
    },
    setCache : function (id, value){
        var path = window.location.pathname;
        var reg = new RegExp( '/' , "g" )
        path = path.replace(reg, "_")
        var jsonStr = localStorage.getItem('cache');
        var json = JSON.parse(jsonStr);
        var data = {};
        if(json){
            json[path][id] = value;
        }else{
            json = {};
            data[id] = value;
            json[path] = data;
        }
        localStorage.setItem('cache' , JSON.stringify(json));
    },
    getCache : function (id) {
        var path = window.location.pathname;
        var reg = new RegExp( '/' , "g" )
        path = path.replace(reg, "_")
        var json = localStorage.getItem('cache');
        if(json){
            var jsonStr = JSON.parse(json);
            return jsonStr[path][id];
        }
    },
    removeCache : function () {
        localStorage.removeItem('cache');
    },
    post : function (url, options , successCallback , errorCallback , isLoad) {
        isLoad == undefined || isLoad ? this.loading() : false;
        setTimeout(function(){
            $.post(url,options,function(result){
                isLoad == undefined || isLoad ? utils.loading(false) : false;
                if(successCallback){
                    successCallback(result);
                }
            });
        }, 100)
    }
}